<template>
	<view class="page">
		<u-cell-group >
			<u-cell  title="账号" :value="userName"></u-cell>
			<picker mode="selector" :range="sexList":value="sexIndex" range-key="name" @change="onChangeSex">
				<u-cell title="性别" :value="sexList[sexIndex].name" :isLink="true" rightIconStyle="font-size: 30rpx;"></u-cell>
			</picker>
			<picker mode="date" :value="birthday" @change="onChangeBirthday">
				<u-cell title="生日" :value="birthday" :isLink="true" rightIconStyle="font-size: 30rpx;"></u-cell>
			</picker>
		</u-cell-group>
		<u-gap height="40" bgColor="#ffffff"></u-gap>
		<u-button text="保存信息" color="#FC2422" @click="submit"></u-button>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import {getMemberApi,saveUserInfoApi} from '@/api/user.js'
	export default {
		computed: {
			...mapState(['user_id','token'])
		},
		data() {
			return {
				userName: '',
				sexList: [
					{
						name: '保密',
						value: 0
					},
					{
						name: '男',
						value: 1
					},
					{
						name: '女',
						value: 2
					}
				],
				sexIndex: 0,
				birthday: '1970-01-01'
			};
		},
		onShow() {
			this.getUserInfo()
		},
		methods: {
			async getUserInfo() {
				const res = await getMemberApi({
					user_id: this.user_id,
					token: this.token
				})
				const {user_name = '',birthday = '',sex = 0} = res.data.info
				this.userName = user_name
				this.birthday = birthday
				this.sexIndex = parseInt(sex)
			},
			async submit() {
				const res = await saveUserInfoApi({
					user_id: this.user_id,
					token: this.token,
					birthdaytime: this.birthday,
					sex: this.sexList[this.sexIndex].value
				})
				uni.showToast({
					title: '保存成功'
				})
				console.log(res)
			},
			onChangeSex(e) {
				this.sexIndex = parseInt(e.detail.value)
			},
			onChangeBirthday(e) {
				this.birthday = e.detail.value
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
